/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aviso.controller;

import aviso.entity.Advert;
import aviso.entity.AdvertQuery;
import aviso.entity.ImageFile;
import aviso.entity.MarkerListWraper;
import aviso.facade.AdvertFacade;
import aviso.facade.UserNameFacade;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ValueChangeEvent;

/**
 *
 * @author avgusti
 */
@ManagedBean
@ViewScoped
public class AdvertController implements Serializable {

    /** Creates a new instance of AdvertController */
    public AdvertController() {
    }

    @PostConstruct
    private void init() {
        newAdvert.setUser(fc.getUser());
        if (newAdvert.getFiles() == null) {
            newAdvert.setFiles((List) new ArrayList<Advert>());
        }
    }
    @EJB
    private AdvertFacade advertFacade;
    @EJB
    private UserNameFacade userFacade;
    @ManagedProperty(value = "#{frontController}")
    private FrontController fc;
    @ManagedProperty(value = "#{addressLookup}")
    private AdderssLookup adderssLookup;
    @ManagedProperty(value = "#{advertQueryController}")
    AdvertQueryController advertQueryController;
    private Advert newAdvert = new Advert();
    private boolean showDialog = false;
    private List<Advert> adverts = new ArrayList();
    private List<Advert> useradverts = new ArrayList();
    private List<Advert> userhistoryadverts = new ArrayList();
    private List<Advert> usercartadverts = new ArrayList();
    private boolean newadverts = true;
    private boolean newcart = true;
    private boolean newhistory = true;
    private List<Advert> advs;
    public FrontController getFc() {
        return fc;
    }

    public void setFc(FrontController fc) {
        this.fc = fc;
    }

    public boolean isNewadverts() {
        return newadverts;
    }

    public void setNewadverts(boolean newadverts) {
        this.newadverts = newadverts;
    }

    public boolean isNewcart() {
        return newcart;
    }

    public void setNewcart(boolean newcart) {
        this.newcart = newcart;
    }

    public boolean isNewhistory() {
        return newhistory;
    }

    public void setNewhistory(boolean newhistory) {
        this.newhistory = newhistory;
    }

    public List<Advert> getUsercartadverts() {
        if (newcart && fc.getUser().getId() != null) {
            usercartadverts = advertFacade.listAdvertsInUserCart(fc.getUser());
            newcart = false;
        }
        return usercartadverts;
    }

    public void setUsercartadverts(List<Advert> usercartadverts) {
        this.usercartadverts = usercartadverts;
    }

    public List<Advert> getUserhistoryadverts() {
        if (newhistory && fc.getUser().getId() != null) {
            userhistoryadverts = advertFacade.listAdvertsInUserhistory(fc.getUser());
            newhistory = false;
        }
        return userhistoryadverts;
    }

    public void setUserhistoryadverts(List<Advert> userhistoryadverts) {
        this.userhistoryadverts = userhistoryadverts;
    }

    public List<Advert> getUseradverts() {
        if (newadverts) {
            useradverts = advertFacade.listByUser(fc.getUser());
            newadverts = false;
        }
        return useradverts;
    }

    public void setUseradverts(List<Advert> useradverts) {
        this.useradverts = useradverts;
    }

    public List<Advert> getAdverts() {
        return adverts;
    }

    public AdvertQueryController getAdvertQueryController() {
        return advertQueryController;
    }

    public void setAdvertQueryController(AdvertQueryController advertQueryController) {
        this.advertQueryController = advertQueryController;
    }

    public void setAdverts(List<Advert> adverts) {
        this.adverts = adverts;
    }
    //Add Advert

    public Advert getNewAdvert() {
        if (newAdvert.getFiles() == null) {
            newAdvert.setFiles((List) new ArrayList<Advert>());
        }
        return newAdvert;
    }

    public void setNewAdvert(Advert newAdvert) {
        if (newAdvert.getFiles() == null) {
            newAdvert.setFiles((List) new ArrayList<Advert>());
        }
        this.newAdvert = newAdvert;
    }

    public void loadNewAdvert(Long id) {
        if (id != null) {
            newAdvert = advertFacade.find(id);
        }
    }

    public void loadNewAdvert(ValueChangeEvent e) {
        if (newAdvert != null) {
            newAdvert.setId((Long) e.getNewValue());
            newAdvert = advertFacade.find(newAdvert.getId());
        }
    }

    public void advertClear() {
        newAdvert = new Advert();
        newAdvert.setUser(fc.getUser());
    }

    public void advertSave() {
        newadverts = true;
        if (newAdvert.getId() != null) {
            advertFacade.edit(newAdvert);
            return;
        }
        newAdvert.setAdded_Date(Calendar.getInstance());
        advertFacade.create(newAdvert);
    }

    public void advertRemove() {
        advertRemove(newAdvert);
    }

    public void advertRemove(Advert a) {
        advertFacade.remove(a);
        newadverts = true;
    }

    public List<Advert> advertList() {
        return advertFacade.findAll();
    }
    
    
    public List<Advert> loadZeroLocatrion()
    {
        if(advs==null)
             advs=advertFacade.loadZeroLocation();
        return advs;
    }
    
    public boolean isShowDialog() {
        return showDialog;
    }

    public void setShowDialog(boolean showDialog) {
        this.showDialog = showDialog;
    }

    public void showDialog(boolean showDialog) {
        this.showDialog = showDialog;
    }

    public AdderssLookup getAdderssLookup() {
        return adderssLookup;
    }

    public void setAdderssLookup(AdderssLookup adderssLookup) {
        this.adderssLookup = adderssLookup;
    }

    public void addFile(ImageFile file) {
        if (!newAdvert.getFiles().contains(file)) {
            newAdvert.getFiles().add(file);
        }
    }

    public void removeFile(ImageFile file) {
        newAdvert.getFiles().remove(file);
    }

    public String advertImage() {
        return newAdvert.advertImage();
    }
boolean test=true;
    public void listAdvertsInBounds() {
        AdvertQuery advertQuery = advertQueryController.getAdvertQuery();
        List<Advert> tmp = advertFacade.lookupAdvertInBounds(advertQuery);
//        List<Advert> filterd = new LinkedList<Advert>();
//        List<Advert> other = new LinkedList<Advert>();
        adverts=tmp;
        
        //filter data
//        int i = 0;
//        Advert perv = null;
//        for (Advert a : filterd) {
//            if (perv == null) {
//                perv = a;
//                i++;
//            }
//            if (!inLocalBounds(a, perv)) {
//                i++;
//                perv = a;
//            }
//            MarkerListWraper<Advert> m = new MarkerListWraper(a);
//            m.setNumber(i);
//            m.setPriority(MarkerListWraper.HIGH);
//            adverts.add(m);
//        }
//        for (Advert a : other) {
//            if (perv == null) {
//                perv = a;
//                i++;
//            }
//            if (!inLocalBounds(a, perv)) {
//                i++;
//            }
//            perv = a;
//            MarkerListWraper<Advert> m = new MarkerListWraper(a);
//            m.setNumber(i);
//            m.setPriority(MarkerListWraper.LOW);
//            adverts.add(m);
//        }
    }

    private boolean inLocalBounds(Advert a, Advert perv) {
        if (Math.abs(a.getLat() - perv.getLat()) <= AdvertQuery.grouping_range && Math.abs(a.getLat() - perv.getLat()) <= AdvertQuery.grouping_range) {
            return true;
        }
        return false;
    }

    public boolean recordAdvertToUserHistoty(Long advID) {
        if (fc.getUser() != null && fc.getUser().getId() != null) {
            fc.setUser(userFacade.addHistoryRercord(fc.getUser().getId(), advID));
            newhistory = true;
            return true;
        }
        return false;
    }

    public boolean recordAdvertToUserCart(Long advID) {
        if (fc.getUser() != null && fc.getUser().getId() != null) {
            fc.setUser(userFacade.addCartRercord(fc.getUser().getId(), advID));
            newcart = true;
            return true;
        }
        return false;
    }

    public boolean removeAdvertFromUserCart(Long advID) {
        if (fc.getUser() != null && fc.getUser().getId() != null) {
            fc.setUser(userFacade.removeCartRercord(fc.getUser().getId(), advID));
            newcart = true;
            return true;
        }
        return false;
    }

    public boolean clearUserCart() {
        if (fc.getUser() != null && fc.getUser().getId() != null) {
            fc.setUser(userFacade.cleanCart(fc.getUser()));
            newcart = true;
            return true;
        }
        return false;
    }

    public boolean clearUserHistory() {
        if (fc.getUser() != null && fc.getUser().getId() != null) {
            fc.setUser(userFacade.cleanHistory(fc.getUser()));
            newhistory = true;
            return true;
        }
        return false;
    }

    public boolean inUserCart(Long advID) {
        if (fc.getUser() != null && fc.getUser().getId() != null) {
            if (fc.getUser().getCart().containsKey(advID)) {
                return true;
            }
        }
        return false;
    }
}
